﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace KnowlegeAssistant
{
    class SubjectUtil
    {
        public static Subject GetSubject(int subject_id)
        {
            Subject subject = null;
            DataTable table = DatabaseConnect.execSP("GetSubjects", new List<string>() { subject_id.ToString() }).Tables[0];
            subject = new Subject(
                (int)table.Rows[0]["subject_id"],
                table.Rows[0]["name"].ToString()
                );
            return subject;
        }

        public static List<Subject> GetAllSubjects()
        {
            List<Subject> subjects = new List<Subject>();
            DataTable table = DatabaseConnect.execSP("GetSubjects").Tables[0];
            Subject subject = null;
            foreach (DataRow r in table.Rows)
            {
                subject = new Subject(
                               (int)r["subject_id"],
                               r["name"].ToString()
                               );
                subjects.Add(subject);
            }
            return subjects;
        }

        public static int AddSubject(string subject_name)
        {
            int subject_id = 0;
            DataTable table = DatabaseConnect.execSP("CreateSubject", new List<string>() { subject_name }).Tables[0];
            subject_id = int.Parse(table.Rows[0]["subject_id"].ToString());
            return subject_id;
        }

        public static void EditSubject(int subject_id, string subject_name)
        {
            DatabaseConnect.execSPNonQuery("UpdateSubject", new List<string>() { subject_id.ToString(),subject_name });
        }

        public static void RemoveSubject(int subject_id)
        {
            DatabaseConnect.execSPNonQuery("RemoveSubject", new List<string>() { subject_id.ToString() });
        }
    }
}
